<template>
  <div class="adminPage">
    <!-- 搜索框 -->
    <div class="search_area">
      <el-form
        :model="params"
        label-width="80px"
        style="display: flex; margin-bottom: 50px"
      >
        <el-form-item label="图书名称">
          <el-input
            clearable
            placeholder="请输入图书名称"
            v-model="params.name"
            style="width: 200px"
            prefix-icon="el-icon-search"
          />
        </el-form-item>
        <el-form-item label="作者">
          <el-input
            clearable
            placeholder="请输入作者"
            v-model="params.author"
            style="width: 200px"
            prefix-icon="el-icon-search"
          />
        </el-form-item>
        <el-form-item>
          <div class="search-buttons">
            <el-button type="warning" style="margin-left: 10px" @click="search"
              >查询</el-button
            >
            <el-button
              type="primary"
              style="margin-left: 10px"
              @click="addUser"
              v-if="
                $store.state.userInfo.role === 'ROLE_ADMIN' ||
                $store.state.userInfo.role === 'ROLE_USER'
              "
              >新增</el-button
            >
          </div>
        </el-form-item>
      </el-form>
    </div>

    <!--表单数据-->
    <div>
      <el-table
        :data="tableData"
        style="width: 100%"
        :header-cell-style="{
          background: '#fafafa',
          color: '#262626',
          fontSize: '14px',
          fontWeight: '500',
          height: '10px',
        }"
      >
        <el-table-column prop="img" label="图书封面">
          <template v-slot="scope">
            <el-image
              style="
                width: 100px;
                height: 100px;
                object-fit: cover;
                border-radius: 50%;
              "
              :src="'http://localhost:8888/api/files/' + scope.row.img"
              :preview-src-list="[
                'http://localhost:8888/api/files/' + scope.row.img,
              ]"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="图书名称"> </el-table-column>
        <el-table-column label="图书介绍">
          <template v-slot="scope">
            <el-button @click="previewDescription(scope.row)">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="author" label="图书作者"> </el-table-column>
        <el-table-column prop="price" label="图书价格" width="100px">
        </el-table-column>
        <el-table-column
          prop="num"
          label="剩余数量"
          width="100px"
        ></el-table-column>
        <el-table-column prop="press" label="图书出版社"> </el-table-column>
        <!-- 状态 -->
        <el-table-column label="状态" width="80px">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status == '1' ? 'success' : 'danger'">
              {{ scope.row.status == "1" ? "启售" : "禁售" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="typeName" label="图书分类"> </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="updateBook(scope.row)"
              v-if="
                $store.state.userInfo.role === 'ROLE_ADMIN' ||
                $store.state.userInfo.role === 'ROLE_USER'
              "
              >编辑</el-button
            >
            <el-popconfirm
              title="确定删除吗？"
              @confirm="deleteBook(scope.row)"
              v-if="
                $store.state.userInfo.role === 'ROLE_ADMIN' ||
                $store.state.userInfo.role === 'ROLE_USER'
              "
            >
              <el-button slot="reference" type="warning">删除</el-button>
            </el-popconfirm>
            <el-button type="warning" @click="downImag(scope.row)"
              >下载</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="block" style="margin-top: 20px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pageNo"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 新增书籍/修改书籍 抽屉 -->
    <el-drawer
      :title="drawerTitle"
      :visible.sync="drawerVisible"
      :direction="direction"
      :before-close="handleClose"
    >
      <book-drawer
        :isEdit="isEdit"
        :record="currentData"
        @success="onSuccess"
        @close="drawerVisible = false"
      ></book-drawer>
    </el-drawer>

    <el-dialog
      title="图书介绍"
      :visible.sync="editorVisiable"
      width="50%"
      @close="editorVisiable = false"
    >
      <div v-html="currentData.content" class="w-e-text"></div>
    </el-dialog>
  </div>
</template>

<script>
import { booklist, deleteBook } from "../../../api/sys/book";
import BookDrawer from "./components/BookDrawer.vue";

export default {
  name: "BookInfo",
  components: { BookDrawer },
  data() {
    return {
      //分页查询参数
      params: {
        pageSize: 10,
        pageNo: 1,
        name: "",
        author: "",
      },
      tableData: [], //表单数据
      loading: "", //加载状态
      total: "", //数据条数
      isEdit: false, //是否是编辑状态
      drawerVisible: false,
      drawerTitle: "", //抽屉标题
      currentData: {}, //当前行数据
      editorVisiable: false,
    };
  },
  created() {
    this.loadData();
  },
  methods: {
    //加载书籍数据
    async loadData() {
      this.loading = true;
      try {
        const res = await booklist(this.params);
        if (res.code === 200) {
          this.tableData = res.data.list || [];
          this.total = res.data.total || 0;
        } else if (res.code === 10001) {
          this.$message.error(res.message);
          //清理登录信息
          this.$store.dispatch("Logout");
        }
      } finally {
        this.loading = false;
      }
    },
    //根据输入模糊查询
    search() {
      this.params.pageNo = 1;
      this.loadData();
    },
    //页码尺寸变化
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.params.pageNo = 1;
      this.loadData();
    },
    //当前页变化
    handleCurrentChange(pageNo) {
      this.params.pageNo = pageNo;
      this.loadData();
    },
    //新增书籍
    addUser() {
      this.currentData = {};
      this.drawerVisible = true;
      this.drawerTitle = "新增书籍";
    },

    //删除书籍
    deleteBook(data) {
      this.$confirm("此操作将永久删除该书籍, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        const id = data.id;
        deleteBook(id).then((res) => {
          if (res.code === 200) {
            this.$message.success("删除成功");
            this.loadData();
          }
        });
      });
    },
    //下载封面图片
    downImag(data) {
      location.href = "http://localhost:8888/api/files/" + data.img;
    },

    //修改书籍
    updateBook(data) {
      this.isEdit = true;
      this.drawerVisible = true;
      this.drawerTitle = "修改书籍";
      this.currentData = data; //当前行数据
      console.log(this.currentData);
    },
    //预览图书介绍
    previewDescription(data) {
      console.log(data);
      this.currentData = data;
      this.editorVisiable = true;
    },
    //成功
    onSuccess() {
      this.loadData();
    },
  },
};
</script>

<style>
.adminPage {
  min-height: 100%;
  background-color: transparent;
}

.el-button {
  margin: 5px;
}
</style>